<template>
  <div class="app-container">
    <div class="card-box">
      <el-table
        v-loading="supplierAgentStore.loading.list"
        :data="supplierAgentStore.list"
        row-id="id"
      >
        <el-table-column label="ID" prop="id" />
        <el-table-column label="名称" prop="name" />
        <el-table-column label="联系人" prop="contact_name" />
        <el-table-column label="联系电话" prop="contact_phone" />
        <el-table-column label="联系邮箱" prop="contact_email" />
        <el-table-column label="是否有效">
          <template #default="{ row }">
            <el-tag :type="row.is_active ? 'success' : 'danger'">
              {{ row.is_active ? "有效" : "失效" }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="{ row }">
            <el-button type="primary" link @click="handleToTransaction(row)">交易明细</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        v-model:current-page="params.page"
        v-model:page-size="params.size"
        style="margin-top: 10px"
        background
        :total="supplierAgentStore.totalCount"
        @change="supplierAgentStore.fetchList(params)"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
import { type SupplierAgentListQuery, type SupplierAgentItemType } from "@/api/supplier/agent";
import router from "@/router";
import { useSupplierAgentStoreHook } from "@/store/modules/supplier/agent";

const supplierAgentStore = useSupplierAgentStoreHook();

const params = reactive<SupplierAgentListQuery>({
  page: 1,
  size: 10,
});

function handleToTransaction(row: SupplierAgentItemType) {
  router.push({
    name: "SupplierTransactions",
    query: {
      supplier_id: row.id,
    },
  });
}

onMounted(() => {
  supplierAgentStore.fetchList(params);
});
</script>

<style scoped lang="scss"></style>
